package com.scpii.universal.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.PageNavigationDto;
import com.scpii.universal.dto.UserInfoDto;
import com.scpii.universal.hibernate.dao.AppDao;
import com.scpii.universal.hibernate.dao.AppIconDao;
import com.scpii.universal.hibernate.dao.AppPageDao;
import com.scpii.universal.hibernate.dao.AppStyleImgDao;
import com.scpii.universal.hibernate.dao.AppUpdateDao;
import com.scpii.universal.hibernate.dao.PageNavigationDao;
import com.scpii.universal.hibernate.entity.AppPageTab;
import com.scpii.universal.hibernate.entity.AppStyleImgTab;
import com.scpii.universal.hibernate.entity.AppTab;
import com.scpii.universal.hibernate.entity.AppUpdateTab;
import com.scpii.universal.hibernate.entity.PageNavigationTab;
import com.scpii.universal.service.PageNavigationService;

/**
 * universalWeb
 * @author liuhj
 * 2012-7-14下午05:01:32
 */
@Service
public class PageNavigationServiceImpl extends
		BaseServiceImpl<PageNavigationTab, Integer> implements
		PageNavigationService {

	@Resource
	private PageNavigationDao pageNavigationDao;
	@Resource
	private AppPageDao appPageDao;
	@Resource
	private AppIconDao appIconDao;
	@Resource
	private AppDao appDao;
	@Resource
	private AppUpdateDao appUpdateDao;
	@Resource
	private AppStyleImgDao appStyleImgDao;

	@Resource
	public void setBaseDao(PageNavigationDao pageNavigationDao) {
		super.setBaseDao(pageNavigationDao);
	}

	@Override
	public Integer saveOrUpdate(PageNavigationTab pageNavigationTab, UserInfoDto userInfo) {
		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		if (pageNavigationTab.getIntNavigateId() > 0 ) {
			//更新导航信息
			StringBuffer hql = new StringBuffer("update PageNavigationTab p set ");
			hql.append("p.datUpdateDatetime=:datUpdateDatetime");
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("datUpdateDatetime", date);//date
			hql.append(",p.varNavigateName=:varNavigateName");
			argsMap.put("varNavigateName", pageNavigationTab.getVarNavigateName());//name
			hql.append(",p.intNavigateOrder=:intNavigateOrder");
			argsMap.put("intNavigateOrder", pageNavigationTab.getIntNavigateOrder());//order
			if ( pageNavigationTab.getIntNavigateIconId() != -1 ) {
				hql.append(",p.intNavigateIconId=:intNavigateIconId");
				argsMap.put("intNavigateIconId", pageNavigationTab.getIntNavigateIconId());//icon
			}
			hql.append(" where p.intNavigateId=:intNavigateId");
			argsMap.put("intNavigateId", pageNavigationTab.getIntNavigateId());
			pageNavigationDao.hqlOperation(hql.toString(), argsMap);
			//更新导航对应页面的标题
			StringBuffer hqlTitle = new StringBuffer("update AppPageTab a set ");
			hqlTitle.append("a.datUpdateDatetime=:datUpdateDatetime,");
			hqlTitle.append("a.varTitleName=:varTitleName");
			hqlTitle.append(" where a.intResourceId=:intResourceId");
			Map<String, Object> argsMapTitle = new HashMap<String, Object>();
			argsMapTitle.put("datUpdateDatetime", date);//date
			argsMapTitle.put("varTitleName", pageNavigationTab.getVarNavigateName());//name
			argsMapTitle.put("intResourceId", pageNavigationTab.getIntResourceId());
			appPageDao.hqlOperation(hqlTitle.toString(), argsMapTitle);
			return pageNavigationTab.getIntNavigateId();
		} else {
			//为导航生成页面
			AppPageTab appPageTab = new AppPageTab();
			appPageTab.setIntAppId(userInfo.getAppId());//应用ID
			appPageTab.setIntEnterpId(userInfo.getIntEnterpId());//企业ID
			appPageTab.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
			appPageTab.setDatUpdateDatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date()));
			appPageTab.setIntTitleIconId(0);
			appPageTab.setVarTitleName(pageNavigationTab.getVarNavigateName());
			int pageId = appPageDao.save(appPageTab);
			//保存导航信息
			pageNavigationTab.setIntResourceId(pageId);
			pageNavigationTab.setIntAppId(userInfo.getAppId());//应用ID
			pageNavigationTab.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
			int navIconId = pageNavigationTab.getIntNavigateIconId();
			navIconId = (navIconId == -1) ? 0 : navIconId;
			pageNavigationTab.setIntNavigateIconId(navIconId);
			//导航类型
			//pageNavigationTab.setIntNavigateType(GenericConstants.ICON_TYPE_LOADING_PRODUCE);
			pageNavigationTab.setDatUpdateDatetime(date);
			Integer pageNavId = pageNavigationDao.save(pageNavigationTab);
			return pageNavId;
		}
	}

	@Override
	public List<PageNavigationDto> listAllNavigation(int appId) {
		StringBuffer hql = new StringBuffer("select new PageNavigationTab(");
		hql.append("p.intNavigateId");
		hql.append(",p.varNavigateName");
		hql.append(",p.intNavigateOrder");
		hql.append(",p.intResourceId");
		hql.append(",p.intNavigateIconId)");
		hql.append(" from PageNavigationTab p");
		hql.append(" where p.intDeleteSign=:intDeleteSign");
		hql.append(" and p.intAppId=:intAppId order by p.intNavigateOrder asc");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		argsMap.put("intAppId", appId);
		List<PageNavigationTab> navigations = pageNavigationDao.hqlGetList(hql
				.toString(), argsMap);
		//获取导航风格
		AppTab app = appDao.get(appId);
		int navStyleId = app.getIntNavigatestyleId();
		//获取导航图标背景
		int navBgImgId = -1;
		String navBgImgUrl = "";
		String[] properties = { "intIconId", "dobAppVersion",
				"intLoadingIconId", "intAppBackgImgId",
				"intAppNavigateBgImgId", "intAppTitleBgImgId", "intNavBgImgId" };
		AppUpdateTab appUpd = appUpdateDao.getVersionUpdateInfo(appId, properties, 0);
		int appStyleId = app.getIntAppStyleId();
		if (appUpd.getIntNavBgImgId() != null && appUpd.getIntNavBgImgId() > 0) {
			navBgImgId = appUpd.getIntNavBgImgId();
			navBgImgUrl = appIconDao.getIconUrlById(navBgImgId);
		} else if ( appStyleId != 0) {
			AppStyleImgTab bkStyleImg = appStyleImgDao.getStyleImgById(appStyleId,GenericConstants.STYLE_TYPE_PRESS);
			if (bkStyleImg != null) {
				navBgImgUrl = bkStyleImg.getVarAppStyleImgUrl();
			}
		}
		List<PageNavigationDto> results = new ArrayList<PageNavigationDto>();
		if (navigations != null && navigations.size() > 0) {
			for (PageNavigationTab pageNavigationTab : navigations) {
				PageNavigationDto nav = new PageNavigationDto();
				BeanUtils.copyProperties(pageNavigationTab, nav);
				nav.setVarIconUrl(appIconDao.getIconUrlById(pageNavigationTab
						.getIntNavigateIconId()));
				nav.setIntNavigatestyleId(navStyleId);
				nav.setIntNavBgImgId(navBgImgId);
				nav.setVarNavBgImgUrl(navBgImgUrl);
				results.add(nav);
			}
			return results;
		}
		return null;
	}

	@Override
	public void saveOrUpdatePatch(
			List<PageNavigationTab> lstNavigations, UserInfoDto userInfo, JSONObject json) {
		//更新标题、导航、应用背景图片
		int intAppTitleBgImgId = json.getInt("intAppTitleBgImgId");
		int intAppBackgImgId = json.getInt("intAppBackgImgId");
		int intAppNavigateBgImgId = json.getInt("intAppNavigateBgImgId");
		int navStyleId = json.getInt("intNavigatestyleId");
		int navBgImgId = json.getInt("intNavBgImgId");
		int intAppSecondTitleImgId = json.getInt("intAppSecondTitleImgId");//二级页面标题背景ID
		//更新导航风格
		StringBuffer appHql = new StringBuffer("update AppTab a set");
		appHql.append(" a.intNavigatestyleId=:intNavigatestyleId");
		appHql.append(" where a.intAppId=:intAppId");
		Map<String, Object> appArgsMap = new HashMap<String, Object>();
		appArgsMap.put("intNavigatestyleId", navStyleId);
		appArgsMap.put("intAppId", userInfo.getAppId());
		appDao.hqlOperation(appHql.toString(), appArgsMap);
		
		StringBuffer hql = new StringBuffer("update AppUpdateTab a set ");
		hql.append("a.datUpdateDatetime=:datUpdateDatetime");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("datUpdateDatetime", new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss").format(new Date()));// date
		if (intAppTitleBgImgId > 0) {
			hql.append(",a.intAppTitleBgImgId=:intAppTitleBgImgId");
			argsMap.put("intAppTitleBgImgId", intAppTitleBgImgId);
		}
		if (intAppBackgImgId > 0) {
			hql.append(",a.intAppBackgImgId=:intAppBackgImgId");
			argsMap.put("intAppBackgImgId", intAppBackgImgId);
		}
		if (intAppNavigateBgImgId > 0) {
			hql.append(",a.intAppNavigateBgImgId=:intAppNavigateBgImgId");
			argsMap.put("intAppNavigateBgImgId", intAppNavigateBgImgId);
		}
		if (navBgImgId > 0) {
			hql.append(",a.intNavBgImgId=:navBgImgId");
			argsMap.put("navBgImgId", navBgImgId);
		}
		if (intAppSecondTitleImgId > 0) {
			hql.append(",a.intAppSecondTitleImgId=:intAppSecondTitleImgId");
			argsMap.put("intAppSecondTitleImgId", intAppSecondTitleImgId);
		}
		hql.append(" where a.intAppUpdateId in(");
		AppUpdateTab androidAppUpd = userInfo.getAndroidVersionUpdate();
		AppUpdateTab iosAppUpd = userInfo.getIosVersionUpdate();
		if (androidAppUpd != null) {
			hql.append(androidAppUpd.getIntAppUpdateId()+ ",");
		}
		if (iosAppUpd != null) {
			hql.append(iosAppUpd.getIntAppUpdateId()+ ",");
		}
		hql.deleteCharAt(hql.lastIndexOf(",")).append(")");
		pageNavigationDao.hqlOperation(hql.toString(), argsMap);
		//保存或更新导航信息
		for (PageNavigationTab pageNavigationTab : lstNavigations) {
			this.saveOrUpdate(pageNavigationTab, userInfo);
		}
		//修改session中存放的背景图片ID
		if (androidAppUpd != null) {
			if (intAppTitleBgImgId > 0)
				androidAppUpd.setIntAppTitleBgImgId(intAppTitleBgImgId);
			if (intAppBackgImgId > 0)
				androidAppUpd.setIntAppBackgImgId(intAppBackgImgId);
			if (intAppNavigateBgImgId > 0)
				androidAppUpd.setIntAppNavigateBgImgId(intAppNavigateBgImgId);
			if (intAppSecondTitleImgId > 0)
				androidAppUpd.setIntAppSecondTitleImgId(intAppSecondTitleImgId);
		}
		if (iosAppUpd != null) {
			if (intAppTitleBgImgId > 0)
				iosAppUpd.setIntAppTitleBgImgId(intAppTitleBgImgId);
			if (intAppBackgImgId > 0)
				iosAppUpd.setIntAppBackgImgId(intAppBackgImgId);
			if (intAppNavigateBgImgId > 0)
				iosAppUpd.setIntAppNavigateBgImgId(intAppNavigateBgImgId);
			if (intAppSecondTitleImgId > 0)
				androidAppUpd.setIntAppSecondTitleImgId(intAppSecondTitleImgId);
		}
	}

	@Override
	public void delete(Integer id) {
		//修改导航逻辑删除符
		pageNavigationDao.delete(id);
	}
}
